package com.etoak.train.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.etoak.train.context.LoginMemberContext;
import com.etoak.train.domain.Passenger;
import com.etoak.train.mapper.PassengerMapper;
import com.etoak.train.req.PassengerQueryReq;
import com.etoak.train.req.PassengerSaveReq;
import com.etoak.train.resp.PageResp;
import com.etoak.train.resq.PassengerQueryResp;
import com.etoak.train.service.PassengerService;
import com.etoak.train.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author by 孙有泽
 * @Date 2025/5/10 12:59
 * @Description 旅客service层
 */
@Service
public class PassengerServiceImpl implements PassengerService {

    @Autowired
    private PassengerMapper passengerMapper;

    /*
     * @author 孙有泽
     * @date 2025/5/10 13:08
     * @description 旅客的添加
     */
    @Override
    public void add(PassengerSaveReq passengerSaveReq) {
        DateTime now = DateTime.now();
        Long id = LoginMemberContext.getId();
        Passenger passenger = BeanUtil.copyProperties(passengerSaveReq, Passenger.class);
        if(ObjectUtil.isNull(passengerSaveReq.getMemberId())) {
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passenger.setMemberId(id);
            passengerMapper.save(passenger);
        }else{
            passenger.setUpdateTime(now);
            passengerMapper.update(passenger);
        }
    }
    /*
     * @author 孙有泽
     * @date 2025/5/10 19:34
     * @description 根据会员id查询乘坐人
     */

    @Override
    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq passengerQueryReq) {
/*
        if(ObjectUtil.isNull(passengerQueryReq.getMemberId())) {
            throw new RuntimeException("【会员ID】不能为空");
        }*/
        PageHelper.startPage(passengerQueryReq.getPage(), passengerQueryReq.getSize());
        System.out.println(passengerQueryReq.getMemberId());
        List<Passenger> passengers = passengerMapper.queryList(passengerQueryReq.getMemberId());
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);


        List<PassengerQueryResp> list = BeanUtil.copyToList(passengers, PassengerQueryResp.class);
        System.out.println(list);
        PageResp<PassengerQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    @Override
    public void delete(Long id) {
        passengerMapper.delete(id);
    }
}
